package com.huanmeiqi.demo.server.provider.controller;

import com.huanmeiqi.demo.api.domain.TOrder;
import com.huanmeiqi.demo.server.provider.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author cq.Wang
 * @date 2018/6/11 16:21
 * @description
 */
@RestController
@RequestMapping("orders")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private DiscoveryClient client;

    @GetMapping("{orderId}")
    public TOrder getOrderByOrderId(@PathVariable Long orderId) {
        return orderService.getOrderByOrderId(orderId);
    }

    @GetMapping("/dis")
    public Object discovery() {
        List<String> services = client.getServices();
        for (String service : services) {
            System.out.println("-------" + service + "-------");
        }

        List<ServiceInstance> instances = client.getInstances("SERVER-PROVIDER-8081");
        for (ServiceInstance instance : instances) {
            System.out.println(instance.getServiceId() + "\t" + instance.getHost() + "\t" + instance.getPort() + "\t"
                    + instance.getUri());
        }

        return this.client;

    }
}
